<template>
  <el-card>
    <h2>员工导入</h2>
    <UploadExcel :before-upload="beforeUpload" :on-success="onSuccess" />
  </el-card>
</template>

<script>
import { sysUserBatch } from '@/api/employees.js'
export default {
  name: 'Import',
  methods: {
    beforeUpload() {
      return true
    },
    async onSuccess({ results }) {
      const obj = {
        手机号: 'mobile',
        姓名: 'username',
        入职日期: 'timeOfEntry',
        转正日期: 'correctionTime',
        工号: 'workNumber'
      }
      const newList = results.map((item) => {
        const list = {}
        Object.keys(item).forEach((item2) => {
          list[obj[item2]] = item[item2]
          if (obj[item2] === 'timeOfEntry' || obj[item2] === 'correctionTime') {
            const t1 = new Date(
              (item[item2] - 1) * 24 * 60 * 60 * 1000 - 8 * 60 * 60 * 1000
            )
            t1.setYear(t1.getFullYear() - 70)
            list[obj[item2]] = t1
          }
        })
        return list
      })
      await sysUserBatch(newList)
      this.$message.success('导入成功')
      this.$router.push('/employees')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin: 20px;
  height: 760px;
  h2 {
    text-align: center;
  }
}
</style>
